package com.imooc.controller;

import com.imooc.enums.ErrorMsg;
import com.imooc.enums.YesOrNo;
import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.pojo.vo.CategoryVO;
import com.imooc.pojo.vo.NewItemsVO;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.imooc.utils.CommonJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 逸尘风清
 * @Date: 2021/5/7
 * @Time: 22:23
 * 描述：
 */
@Api(value = "首页接口" , tags = {"首页相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {
    @Resource
    private CarouselService carouselService;

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "获取轮播图列表", notes = "获取轮播图列表")
    @GetMapping("/carousel")
    public CommonJsonResult carousel(@RequestParam @ApiParam(value = "是否展示") Integer isShow){
        // List<Carousel> carousels = carouselService.queryAll(isShow);
        List<Carousel> carousels = carouselService.queryAll(YesOrNo.YES.type);
        return CommonJsonResult.ok(carousels);
    }

    @ApiOperation(value = "获取商品分类（一级分类）列表", notes = "获取商品分类（一级分类）列表")
    @GetMapping("/cats")
    public CommonJsonResult cats(){
        List<Category> categoryList = categoryService.queryAllRootCategory();
        return CommonJsonResult.ok(categoryList);
    }

    @ApiOperation(value = "根据一级分类id查询子分类", notes = "根据一级分类id查询子分类")
    @GetMapping("/subCat/{rootCatId}")
    public CommonJsonResult subCat(
                        @ApiParam(value = "一级分类id", required = true)
                        @PathVariable Integer rootCatId){
        if (rootCatId == null) {
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        List<CategoryVO> subCatList = categoryService.getSubCatList(rootCatId);
        return CommonJsonResult.ok(subCatList);
    }

    @ApiOperation(value = "根据一级分类id获取对应分类6个新产品", notes = "根据一级分类id获取对应分类6个新产品")
    @GetMapping("/sixNewItems/{rootCatId}")
    public CommonJsonResult sixNewItems(
            @ApiParam(value = "一级分类id", required = true)
            @PathVariable Integer rootCatId){
        if (rootCatId == null) {
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        List<NewItemsVO> sixNewItemsLazy = categoryService.getSixNewItemsLazy(rootCatId);
        return CommonJsonResult.ok(sixNewItemsLazy);
    }
}
